package com.it135.citytraffic.task;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.it135.citytraffic.pojo.entity.Crossing;
import com.it135.citytraffic.pojo.entity.RoadCache;
import com.it135.citytraffic.pojo.entity.RoadStatistic;
import com.it135.citytraffic.pojo.entity.UserRoadReason;
import com.it135.citytraffic.service.ICrossingService;
import com.it135.citytraffic.service.IRoadCacheService;
import com.it135.citytraffic.service.IRoadStatisticService;
import com.it135.citytraffic.service.IUserRoadReasonService;
import com.it135.citytraffic.utils.RoadAnalyseUtils;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;

import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;
import java.util.List;

@Component
@Slf4j
@Data
public class AnalyseTask {
    private final ICrossingService iCrossingService;
    private final IRoadStatisticService iRoadStatisticService;
    private final IRoadCacheService iRoadCacheService;
    private final RoadAnalyseUtils roadAnalyseUtils;
    private final IUserRoadReasonService iUserRoadReasonService;

    /**
     * 此方法非常耗时预计至少60分钟(考虑到轻量级服务器的性能较低)
     */

    @Scheduled(cron = "${task.crossingAnalyseScheduled.cron}")
    public void crossingAnalyse() {
        iCrossingService.list().forEach(crossing -> {
            double statusAmount = 0.0;
            LambdaQueryWrapper<RoadCache> roadCacheLambdaQueryWrapper = new LambdaQueryWrapper<>();
            roadCacheLambdaQueryWrapper.eq(RoadCache::getCrossingId, crossing.getId());
            List<RoadCache> roadCacheList = iRoadCacheService.list(roadCacheLambdaQueryWrapper);
            String crossingAnalyseCache;
            String reasonCache;
            String suggestCache;
            StringBuilder analyseBody = new StringBuilder();
            StringBuilder analyseHead = new StringBuilder();
            StringBuilder reasonHead = new StringBuilder();
            StringBuilder reasonBody = new StringBuilder();
            StringBuilder suggestionHead = new StringBuilder();
            StringBuilder suggestionBody = new StringBuilder();

            {
                suggestionHead.append(crossing.getName()).append("的各个路段建议如下:<br>");
                reasonHead.append(crossing.getName()).append("所涉及的路段较多,每个路段不同方向的拥堵原因也不尽相同," +
                        "以下根绝不同路段的拥堵分析进行陈述<br>");
                analyseBody.append(crossing.getName()).append("相关的各个路段分析:<br>");

                for (RoadCache roadCache : roadCacheList) {
                    statusAmount += roadCache.getStatusAverage();
                    analyseBody.append(roadCache.getName()).append(":");
                    analyseBody.append(roadCache.getAnalyseCache()).append("平均拥塞度为");
                    analyseBody.append(roadCache.getStatusAverage()).append("<br>");
                    reasonBody.append(roadCache.getName()).append(":").append(roadCache.getReasonCache()).append("<br>");
                    suggestionBody.append(roadCache.getName()).append(":").append(roadCache.getSuggestionCache()).append("<br>");
                }
                RoadCache max = iRoadCacheService.findMax();
                RoadCache min = iRoadCacheService.findMin();
                analyseHead.append("整个路口的平均拥塞度为").append(statusAmount / roadCacheList.size())
                        .append("其中最为拥堵的路段是").append(max.getName()).append("平均拥塞为:").append(max.getStatusAverage())
                        .append("相对通畅的是").append(min.getName()).append("平均拥塞为:").append(min.getStatusAverage()).append("<br>");
                crossingAnalyseCache = analyseHead.toString() + analyseBody;
                reasonCache = reasonHead.toString() + reasonBody;
                suggestCache = suggestionHead.toString() + suggestionBody;
            }
            Crossing newCrossing = new Crossing();
            newCrossing.setId(crossing.getId());
            newCrossing.setAnalyseCache(crossingAnalyseCache);
            newCrossing.setSuggestion(suggestCache);
            newCrossing.setReasonAnalyseCache(reasonCache);
            newCrossing.setAnalyseLastUpdate(LocalDateTime.now());
            iCrossingService.updateById(newCrossing);
        });
    }

    @Scheduled(cron = "${task.timeAnalyseScheduled.cron}")
    public void roadTimeAnalyse() {
        for (RoadStatistic roadStatistic : iRoadStatisticService.findAll()) {
            roadAnalyseUtils.timeAnalyse(roadStatistic.getCrossingId(), roadStatistic.getName());
        }
    }
    /**
     * 路段原因分析,滨海姐姐过拼接加入路口缓存
     */
    @Scheduled(cron = "${task.reasonAnalyseScheduled.cron}")
    public void roadReasonAnalyse() {
        for (UserRoadReason userRoadReason : iUserRoadReasonService.findAll()) {
            LambdaQueryWrapper<RoadCache> roadCacheLambdaQueryWrapper = new LambdaQueryWrapper<>();
            roadCacheLambdaQueryWrapper.eq(RoadCache::getName, userRoadReason.getRoadName());
            RoadCache roadCache = iRoadCacheService.getOne(roadCacheLambdaQueryWrapper);
            roadAnalyseUtils.reasonAnalyse(roadCache);
        }
    }

    @Scheduled(cron = "${task.suggestionScheduled.cron}")
    public void roadSuggestion() {
        for (RoadCache roadCache : iRoadCacheService.list()) {
            roadAnalyseUtils.roadSuggestion(roadCache);
        }
    }
}
